//
//  BaseJsonRequest.m
//  EasyToo
//
//  Created by zhengzhe on 13-8-24.
//
//

#import "BaseJsonRequest.h"
#import "SBJsonWriter.h"
#import "DeviceUtil.h"

@implementation BaseJsonRequest

- (id)init
{
    [super init];
    if (self != nil)
    {
        self.headerJsonDict = [[NSMutableDictionary alloc] init];
        self.bodyJsonDict = [[NSMutableDictionary alloc] init];
        
        self.method = @"POST";
        
        [self addDefaultHeaderJson];
    }
    return self;
}

- (void)dealloc
{
    self.headerJsonDict = nil;
    self.bodyJsonDict = nil;
    [super dealloc];
}

- (void)buildJsonRequest
{
    SBJsonWriter *writer = [[[SBJsonWriter alloc] init] autorelease];
        
    NSMutableDictionary *jsonDict = [[[NSMutableDictionary alloc] init] autorelease];
    [jsonDict setObject:self.headerJsonDict forKey:@"CTSReqHeader"];
    [jsonDict setObject:self.bodyJsonDict forKey:@"CTSReqBody"];
    
    NSString *jsonString = [writer stringWithObject:jsonDict];
    NSLog(@"jsonbody:%@", jsonString);
    
//    NSString *resultCode = [DeviceUtil getMD5:jsonString];
    
//    [self.headerJsonDict setObject:resultCode forKey:@"ResultCode"];
//    [jsonDict setObject:self.headerJsonDict forKey:@"CTSReqHeader"];
//
//    jsonString = [writer stringWithObject:jsonDict];
//    NSLog(@"jsonbody:%@", jsonString);
    
    [self.dataDict setObject:jsonString forKey:@"jsonstring"];
    
}


- (void)addDefaultHeaderJson
{
    //接入平台渠道，ios填06，android填05，但是目前06有问题
    [self.headerJsonDict setObject:@"05" forKey:@"Channel"];
    //交易代码
    [self.headerJsonDict setObject:@"016006" forKey:@"TranCode"];
    //交易流水号
    [self.headerJsonDict setObject:@"0043130807191513" forKey:@"MesgRefID"];
    //受理端编号，ios可以填机器mac地址或者imei
    [self.headerJsonDict setObject:@"10280043,21218CCA77804D2BA1922C33E0151105" forKey:@"OrigSender"];
    //用户帐号
    [self.headerJsonDict setObject:@"2.0.20120808" forKey:@"Userid"];
    //应答码，手机端可填空
    [self.headerJsonDict setObject:@"" forKey:@"ResultCode"];
    //报文摘要，可填空
    [self.headerJsonDict setObject:@"" forKey:@"Comment"];
    //交易帐户
    [self.headerJsonDict setObject:@"36196859" forKey:@"TxAccount"];
    //交易额度
    [self.headerJsonDict setObject:@"0" forKey:@"TxAmt"];
    //交易联系电话
    NSString *contacter = @"";
    NSString *defaultName = [[NSUserDefaults standardUserDefaults] valueForKey:@"userName"];
    if (defaultName) {
        contacter = defaultName;
    }
    //[self.headerJsonDict setObject:contacter forKey:@"Contacter"];
    [self.headerJsonDict setObject:@"13823642844" forKey:@"Contacter"];
    //页码
    [self.headerJsonDict setObject:@"1" forKey:@"TxPage"];
    //每页条数
    [self.headerJsonDict setObject:@"20" forKey:@"TxRecordNum"];
}

@end
